package com.yckj.ytracker.plugin.mybatis.demo;

import com.yckj.ytracker.plugin.mybatis.demo.dao.UserInfoDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

/**
 * @author liuwei
 * @version 2017/10/19 上午1:03
 * @description 功能描述
 * @see
 * @since 1.0
 */
public class Demo {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            reader = Resources.getResourceAsReader("mapper.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
        demo.selectUsers();
    }

    public void selectUsers() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            UserInfoDao userInfoDao = session.getMapper(UserInfoDao.class);
            System.out.println(userInfoDao.selectUsers());
        } finally {
            session.close();
        }
    }

}
